$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_cscr_m")
    const toolbar = $NG.getCmpApi("toolbar_top")
    const dgrid1 = $NG.getCmpApi("")
    const getHeadValue = (str) => mstform.getItem(str).getValue()

    useValuesChange(() => {
        getXmkInfoByPhid()
        $NG.alert("说明未中标前往中止模块!")
        if (!getHeadValue("u_xmmc")) {
            toolbar.getItem("u_zzmk").setProps({
                hidden: true
            })
        } else {
             toolbar.getItem("u_zzmk").setProps({
                hidden: false
            })
        }

    }, "p_form_cscr_m.u_xmmc")

    useValuesChange(() => {
        mstform.getItem("u_jyhgb").setValue(0 - (Number(mstform.getItem("u_jygb")) || 0))
    }, "p_form_cscr_m.u_jygb")

    useClick(() => {
        window.onModule()
    }, "u_zzmk")

    // 获取项目库信息
    function getXmkInfoByPhid() {
        const phid = getHeadValue("u_xmmc")
        if (!phid) return
        $NG.execServer("getXmkInfoByPhid", { phid }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data)[0].extendObjects
            $NG.updateState((updater) => {
                updater.data.p_form_cscr_m.setProps({
                    bill_no: datas.u_xmbh,


                })
            })
        })
    }


    // 终止模块
    OpenOffWindows()
    async function OpenOffWindows() {
        window.moduleLock = false
        const div = document.createElement("div")
        const mb = document.createElement("div")
        const h3 = document.createElement('h3')
        const frame = await setFrame()
        const body = document.querySelector("body")
        window.frameDom = frame
        window.frameModule = div
        mb.style.position = 'absolute'
        mb.style.zIndex = '998'
        mb.style.width = '100vw'
        mb.style.height = '100vh'
        mb.style.background = '#00000038'
        div.style.width = '70vw'
        div.style.height = '80vh'
        div.style.margin = '10vh 0 0 15vw'
        mb.style.top = '-10vh'
        mb.style.left = '-15vw'

        div.style.position = 'absolute'
        div.style.background = '#fff'
        div.style.borderRadius = '10px'
        div.style.zIndex = '999'
        div.style.display = 'none'
        h3.style.margin = '2% 0 2% 2%'
        mb.onclick = () => {
            window.offModule()
        }
        // h3.textContent = '中止模块'
        div.appendChild(mb)
        div.appendChild(h3)
        div.appendChild(frame)
        body.appendChild(div)
        async function setFrame() {
            let phid = mstform.getItem("u_zzid").getValue()
            const res = await $NG.execServer("SELECT", { sql: `select phid from p_form_zz_m where u_lyid = '${phid}'` })
            if (res.count !== 0) {
                phid = JSON.parse(res.data)[0].extendObjects.phid

            }
            let src = `http://10.52.119.173:30847/react/index.html?v=${new Date().getTime()}#/customform/detail?otype=${!phid ? "add" : "edit&id=" + phid}&busType=zz&isSso=1&AppTitle=%E4%B8%AD%E6%AD%A2-%E6%96%B0%E5%A2%9E&orgId=1&orgName=%E4%B8%AD%E5%9B%BD%E6%B0%B4%E5%88%A9%E6%B0%B4%E7%94%B5%E7%AC%AC%E5%85%AD%E5%B7%A5%E7%A8%8B%E5%B1%80%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8&menucode=5f5f3ffd-10c3-bc6a-5119-9713d93a79bb`
            const frame = document.createElement("iframe")
            frame.style.width = '100%'
            frame.style.height = '100%'
            frame.style.border = '0'
            frame.src = src
            frame.style.position = 'absolute'
            frame.style.zIndex = '999'
            return frame
        }
        // 关闭函数
        window.offModule = () => {
            window.frameModule.style.display = 'none'
            window.frameDom.remove()
        }
        // 开启函数
        window.onModule = async () => {
            if (window.moduleLock) return
            const frame = await setFrame()
            window.frameDom = frame
            window.frameModule.appendChild(frame)
            window.frameModule.style.display = 'block'
            window.moduleLock = false
        }
        // 接收子的信息
        window.moduleChildMessage = (json) => {
            const data = JSON.parse(json)
            if (data.isOk) {
                const datas = data.data
                mstform.getItem("u_zzid").setValue(datas.u_lyid)
            } else {
                $NG.message("中止模块发生异常，请勿保存单据！数据异常！")
            }
            setTimeout(() => {
                window.offModule()
            }, 200)
        }
    }
})
